package com.jims.api.v1.controller.weixin.doctor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class WX_UserUtil {
    private static Logger log = LoggerFactory.getLogger(WX_UserUtil.class);
    /**
     * 根据微信openId 获取用户是否订阅
     * @param openId 微信openId
     * @return 是否订阅该公众号标识
     */
    public static Integer subscribeState(String openId){
        String tmpurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+WX_TokenUtil.getAccessToken() +"&openid="+openId;
        JSONObject result = WX_HttpsUtil.httpsRequest(tmpurl, "GET",null);
        JSONObject resultJson = new JSONObject(result);
        log.error("获取用户是否订阅 errcode:{} errmsg:{}", resultJson.getInteger("errcode"), resultJson.getString("errmsg"));
        String errmsg = (String) resultJson.get("errmsg");
        if(errmsg==null){
            //用户是否订阅该公众号标识（0代表此用户没有关注该公众号 1表示关注了该公众号）。
            Integer subscribe = (Integer) resultJson.get("subscribe");
            return subscribe;
        }
        return -1;
    }
    /*
	 * 通过code获取用户信息的access_token
	 * */
    public static Map<String, String> getUserInfoAccessToken(String code) {
        String FileName = "WxTokenUtil.properties";
        String APPID=null;
        String APPSECRET=null;
        try {
        Properties prop = new Properties();// 属性集合对象
        InputStream fis = WX_UserUtil.class.getClassLoader().getResourceAsStream(FileName);

            prop.load(fis);// 将属性文件流装载到Properties对象中
            fis.close();// 关闭流
            APPID = prop.getProperty("APPID");
            APPSECRET = prop.getProperty("APPSECRET");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取Appid，APPsecret

        Map<String, String> data = new HashMap();
        JSONObject object = null;
        String url = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
                APPID, APPSECRET, code);
        try {
            object =  WX_HttpsUtil.httpsRequest(url, "GET",null);
            data.put("openid", object.get("openid").toString().replaceAll("\"", ""));
            data.put("access_token", object.get("access_token").toString().replaceAll("\"", ""));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    /*
	 * 通过code获取用户信息的access_token
	 * */
    public static Map<String, String> getUnionid(String openid) {
        //获取Appid，APPsecret
        Map<String, String> data = new HashMap();
        JSONObject object = null;
        String access_token=WX_TokenUtil.getAccessToken();
        String url = String.format("https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN",access_token,openid);
        try {
            object =  WX_HttpsUtil.httpsRequest(url, "GET",null);
            data.put("unionid", object.get("unionid").toString().replaceAll("\"", ""));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}
